/**
 * Created by zhaoxi on 2017/12/5.
 */
import {conformsTo, isFunction} from 'lodash';
import invariant from 'invariant';

import modelManager from './ModelManager';
import createReducer from './reducers';

// 验证redux store数据结构
export function checkStore(store) {
    const shape = {
        dispatch: isFunction,
        subscribe: isFunction,
        getState: isFunction,
        replaceReducer: isFunction,
    };
    invariant(
        conformsTo(store, shape),
        '(app/Utils...) 异步注入错误: 不是一个有效的store'
    );
}

export function injectModelReducer(store) {
    return function injectModel(model) {
        modelManager.useModel(model);
        console.log('--------replace model------------');
        store.replaceReducer(createReducer());
    }
}

// 辅助创建injectors
export function getAsyncInjectors(store) {
    checkStore(store);
    return {
        // injectReducer: injectAsyncReducer(store, true),
        // injectSagas: injectAsyncSagas(store, true),
        injectModel: injectModelReducer(store)
    };
}